import Taro from '@tarojs/taro'
import { getToken } from '@/config/auth'

export const apiConfig =
    process.env.NODE_ENV === 'production'
        ? {
              API_BASE_URL: 'https://api.krisli.cn' // 生产环境的请求域名
          }
        : {
              API_BASE_URL: 'http://localhost:3000' // 本地环境的请求域名
          }

export const post = async (url: string, data: any) => {
    const { data: result_data } = await Taro.request({
        method: 'POST',
        url: apiConfig.API_BASE_URL + url,
        data,
        header: {
            Authorization: 'Bearer ' + getToken()
        }
    })

    if (result_data.code !== 0) {
        throw new Error(result_data.message)
    }

    return result_data
}
